package org.xilin

class SemesterController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list")
    }

    def list = {
        def list = Semester.list(params)
        render(template: "list", model: [semesterInstanceList: list, semesterInstanceTotal: Semester.count()])
    }

    def create = {
        def semesterInstance = new Semester()
        semesterInstance.properties = params
        render(template: "create", model: [semesterInstance: semesterInstance])
    }

    def save = {
        def semesterInstance = null

        if (params.id) {
            semesterInstance = Semester.get(params.id)
            semesterInstance.properties = params
        }
        else {
            semesterInstance = new Semester(params)
        }
      
        def semester = Semester.findByYearAndSemester(params.year, params.semester)

        if (semester) {
          println ">>>> duplicate data"
          semesterInstance.errors.rejectValue('year', 'Semester already exist') 

          flash.message = "Semester already exists"
          render(template: params.view, model: [semesterInstance: semesterInstance])
          return
        }

        println "principal = " + session.principal
        println "principal.class.name = " + session.principal.class.name
        semesterInstance.updatedBy = User.findByUsername(session.user)
        if (!semesterInstance.validate()) {
            flash.message = "Missing required data"
            semesterInstance.errors.allErrors.each {
                println it
            }
            render(template: params.view, model: [semesterInstance: semesterInstance])
            return
        }

        if (!semesterInstance.save(flush: true)) {
            semesterInstance.errors.allErrors.each {
                log.debug(it)
            }
            flash.message = "Database error"
            render(template: params.view, model: [semesterInstance: semesterInstance])
            return
        }
        redirect(action: "list")
    }

    def show = {
        def semesterInstance = Semester.get(params.id)
        if (!semesterInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'semester.label', default: 'Semester'), params.id])}"
            redirect(action: "list")
        }
        else {
            render(template: "show", model: [semesterInstance: semesterInstance])
        }
    }

    def edit = {
        def semesterInstance = Semester.get(params.id)
        if (!semesterInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'semester.label', default: 'Semester'), params.id])}"
            redirect(action: "list")
        }
        else {
            render(template: "edit", model: [semesterInstance: semesterInstance])

        }
    }

    def delete = {
        def semesterInstance = Semester.get(params.id)
        if (semesterInstance) {
            try {
                semesterInstance.delete(flush: true)
                //flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'semester.label', default: 'Semester'), params.id])}"
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'semester.label', default: 'Semester'), params.id])}"
            }
            catch(org.hibernate.exception.ConstraintViolationException e) {
                flash.message = "Book '${bookInstance.title}' could not be deleted"
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'semester.label', default: 'Semester'), params.id])}"
        }
        redirect(action: "list")
    }
}
